package com.zadtek.shopmall.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.Event.EventPoiAddress;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.other.LocationManger;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.List;

public class ChoosePoiAddressActivity extends AbsActivity implements View.OnClickListener, PoiSearch.OnPoiSearchListener {
    private ImageView icback;
    private TextView topTitle;
    private EditText etSearch;
    private TextView tvLocation;
    private RecyclerView recyclerView;
    private PoiAdapter poiAdapter;
    private List<PoiItem> data = new LinkedList<>();
    private int currentPage = 1;

    @Override
    public int setView() {
        return R.layout.activity_choose_poi_address;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
    }

    private void initLogic() {
        icback.setOnClickListener(this);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String searchStr = s.toString();
                searchPoi(searchStr);
            }
        });
        poiAdapter = new PoiAdapter(R.layout.poi_address_item,data);
        recyclerView.setAdapter(poiAdapter);
        LocationManger.fetch(new LocationManger.OnLocationListener() {
            @Override
            public void onLocation(AMapLocation location) {
                String address = location.getCity()+location.getDistrict()+location.getStreet()+location.getAoiName();
                tvLocation.setText(address);
            }
        });
        poiAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PoiItem item = data.get(position);
                String address = item.getCityName()+item.getAdName()+item.getSnippet()+item.getTitle();
                EventBus.getDefault().post(new EventPoiAddress(address));
                finish();
            }
        });
        tvLocation.setOnClickListener(this);
    }

    private void searchPoi(String searchStr) {
        PoiSearch.Query query = new PoiSearch.Query(searchStr, "", LocationManger.location.getCity());
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(currentPage);//设置查询页码
        PoiSearch poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    private void initView() {
        icback = findViewById(R.id.ic_back);
        icback.setVisibility(View.VISIBLE);
        topTitle = findViewById(R.id.title);
        topTitle.setText("选择地址");
        tvLocation = findViewById(R.id.tv_location);
        etSearch = findViewById(R.id.et_search);
        recyclerView = findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ic_back:
                finish();
                break;
            case R.id.tv_location:
                String address = tvLocation.getText().toString();
                EventBus.getDefault().post(new EventPoiAddress(address));
                finish();
                break;
        }
    }


    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        if(i == 1000){
            data.clear();
            data.addAll(poiResult.getPois());
            poiAdapter.notifyDataSetChanged();
        }else{
            //ToastUtil.showToast(""+i);
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    private class PoiAdapter extends BaseQuickAdapter<PoiItem,BaseViewHolder>{
        public PoiAdapter(int layoutResId, @Nullable List<PoiItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, PoiItem item) {
            String address = item.getCityName()+item.getAdName()+item.getSnippet()+item.getTitle();
            helper.setText(R.id.poi_address,address);
        }
    }
}
